package com.ruoyi.edh.mapper;

import com.ruoyi.edh.domain.EdhMicroshopGoods;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


/**
 * 易订货-微店上架商品Mapper接口
 *
 * @author xxx
 * @date 2020-09-02
 */
public interface EdhMicroshopGoodsMapper {
	/**
	 * 获取微店最新上架的前六款产品
	 */
	public List<Map<String, Object>> selectMicroShopGoodsByShopId(Long shopId);


	/**
	 * 获取微店最新上架商品
	 */
	public List<Map<String, Object>> selectMicroShopGoodsByShopIdAndGoodsName(@Param("shopId") Long shopId, @Param("goodsName") String goodsName);

	/**
	 * 某人的微店的商品列表
	 *
	 * @param title    商品名称
	 * @param userId   微店用户id
	 * @param sortType 排序类别 1奖励最高 2最新发布 3价格升序 4价格降序
	 * @return 易订货-微店上架商品集合
	 */
	public List<Map<String, Object>> selectEdhMicroshopGoodsList(@Param("title") String title, @Param("sortType") Integer sortType, @Param("userId") Long userId);

	/**
	 * 平台查询团长的商品列表
	 *
	 * @param title  商品名称
	 * @param userId 微店用户id
	 */
	public List<Map<String, Object>> getList(@Param("userId") Long userId, @Param("title") String title, @Param("onSale") String onSale);


	/**
	 * 查询微店可以上架的商品
	 *
	 * @param title    商品名称
	 * @param userId   微店用户id
	 * @param sortType 排序类别 1奖励最高 2最新发布 3价格升序 4价格降序
	 * @return 易订货-微店上架商品集合
	 */
	public List<Map<String, Object>> reselllist(@Param("title") String title, @Param("sortType") Integer sortType, @Param("userId") Long userId);

	/**
	 * 新增易订货-微店上架商品
	 *
	 * @param goods  商品id
	 * @param shopId 添加商品的店铺
	 * @return 结果
	 */
	public int insertEdhMicroshopGoods(@Param("goods") List<Long> goods
			, @Param("shopId") Long shopId
			, @Param("userId") Long userId);


	/**
	 * 修改易订货-微店上架商品
	 *
	 * @param edhMicroshopGoods 易订货-微店上架商品
	 * @return 结果
	 */
	public int updateEdhMicroshopGoods(EdhMicroshopGoods edhMicroshopGoods);


	/**
	 * 重新上架
	 */
	public Integer onSale(@Param("goods") List<Long> goods, @Param("microshopId") Long microshopId);

	/**
	 * 删除易订货-微店上架商品
	 *
	 * @param id 易订货-微店上架商品ID
	 * @return 结果
	 */
	public int deleteEdhMicroshopGoodsById(Long id);

	/**
	 * 批量删除易订货-微店上架商品
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	public int deleteEdhMicroshopGoodsByIds(Long[] ids);

	/*********************************************************************************************/
	/**
	 * 获取下架或者删除的商品集合
	 */
	@Select("select goods_id from edh_microshop_goods  where microshop_id = #{shopId} and (on_sale = 0 or del_flag = 1) ")
	public List<Long> getNotOnSale(Long shopId);

	/**
	 * 查询所有可以全部上架的商品id
	 */
	@Select("select id from edh_goods where del_flag = 0 and resell = 1")
	public List<Long> getAllReSellGoods();

	/**
	 * 根据微店商品id查询店铺id和商品id
	 */
	@Select("select microshop_id shopId , goods_id goodsId from edh_microshop_goods  where id = #{microShopGoodsId} ")
	public Map<String, Long> loadID(Long microShopGoodsId);

	/**
	 * 设置微店商品状态为无效
	 */
	@Update("UPDATE   edh_microshop_goods set on_sale = 2 WHERE goods_id in (SELECT goods_id from edh_scheduled_goods where scheduled_id  not in  (SELECT id from edh_scheduled where NOW() BETWEEN scheduled_start_time and scheduled_end_time))")
	public int killMicroGoods();

	/**
	 * 获取失效相关的微店商品id
	 */
	@Select("select  id from edh_microshop_goods WHERE goods_id in (SELECT goods_id from edh_scheduled_goods where scheduled_id  not in  (SELECT id from edh_scheduled where NOW() BETWEEN scheduled_start_time and scheduled_end_time))")
	public List<Long> getMicroGoodsByScheduled();

	@Update("update edh_microshop_goods set del_flag =1 where microshop_id = #{mshopId} and goods_id = #{goodsId}")
	int deleteEdhMicroshopGoodsByMshopIdAndGodosId(@Param("mshopId") Long mshopId, @Param("goodsId") Long goodsId);
}
